package gruppoSoluzione.mainService.data;

import gruppoSoluzione.mainService.ServiceDB;
import gruppoSoluzione.mainService.ServiceDBLog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 * Responsabilità: Gestione del Database Effettivo
 * 
 * @author Daniel Procop, Mattia Grecchi
 */
public class ServiceDBXML {

	public static final String DB_LOCATION = "db";

	private String dbLocation = DB_LOCATION;

	private ServiceDBData dataStorer = new ServiceDBData();

	public ServiceDBXML() {

	}

	public ServiceDBXML(String dbLocation) {
		super();
		this.dbLocation = dbLocation;
	}

	/**
	 * Carica il DB
	 * 
	 * @param toBeFilled
	 */
	public void loadDB(ServiceDB toBeFilled) {
		

		File file = new File(dbLocation);
		ArrayList<ServiceDBLog> allLogs = loadLogFile(file);
		toBeFilled.addLogs(allLogs);
		
	}

	/**
	 * Salva il db
	 * 
	 * @param toBeWritten
	 */
	public void saveDB(ServiceDB toBeWritten) {

		

			try {
				OutputStreamWriter fileWriter = new OutputStreamWriter(
						new FileOutputStream(dbLocation),
						Charset.forName("UTF-8").newEncoder());

				dataStorer.writeDB(new WriterStringWriter(fileWriter),
						toBeWritten.getLogsIterable());

				fileWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		

	}

	private ArrayList<ServiceDBLog> loadLogFile(File fileXml) {

		ArrayList<ServiceDBLog> logs = new ArrayList<ServiceDBLog>();

		try {

			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document document = dBuilder.parse(fileXml);

			NodeList registro = document.getElementsByTagName("log");
			for (int i = 0; i < registro.getLength(); i++) {
				Element libro = (Element) registro.item(i);
				String operatore = libro.getElementsByTagName("operatore").item(0)
						.getTextContent();
				String data = libro.getElementsByTagName("data").item(0)
						.getTextContent();
				String ore = libro.getElementsByTagName("ore")
						.item(0).getTextContent();
				String task = libro.getElementsByTagName("task")
						.item(0).getTextContent();

				ServiceDBLog baseLog = new ServiceDBLog(data, operatore, Integer.parseInt(ore), task);
				logs.add(baseLog);
			}

		} catch (ParserConfigurationException e) {
			/*
			 * TODO qui andare su console pu?? essere ok, perch?? ?? la console
			 * del server a cui soltanto gli operatori accedono e non gli
			 * utenti, ma si pu?? sempre considerare di rivalutare la questione
			 */
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return logs;
	}

}
